#!/usr/bin/env node
/*
 *   Copyright (C) 2017 Igor Konovalov
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

'use strict';
const account = require('./../cmd-account');
const ipfs = require('./../cmd-ipfs');
const config = require('./../cmd-config');
const colors = require('colors');
const program = require('commander');

program
    .version(require('../package.json').version)
    .usage('<command>');

// ACCOUNT CMD
program
    .command('account <subCommand> [arg]')
    .usage('<subCommand> [arg]')
    .description(
        'Ethereum account operations.' +
        '\n\t Subcommands:' +
        '\n\t\t ls [balance]' +
        '\n\t\t keys <index or address>'.red
    )
    .action((subCommand, arg, options) => {
        switch (subCommand) {
            case account.ls.name: {
                account.ls(arg, options);
                break;
            }
            case account.keys.name: {
                account.keys(arg, options);
                break;
            }
            default: {
                console.log(`Command 'account ${subCommand}' not found.`);
            }
        }
    });

// IPFS CMD
program
    .command('ipfs <subCommand> <arg> [arg2]')
    .usage('<subCommand> <arg> [arg2]')
    .description(
        'IPFS files and tags operations.' +
        '\n\t Subcommands: ' +
        '\n\t\t add <file-path>' +
        '\n\t\t cat <any-ipfs-hash>' +
        '\n\t\t ecTag <ecTag-ipfs-hash>' +
        '\n\t\t decryptEcTag <ecTag-ipfs-hash>'.red +
        '\n\t\t decrypt <ecTag-ipfs-hash>' +
        '\n\t\t signature <ecTag-ipfs-hash>' +
        '\n\t\t delegate <ecTag-ipfs-hash> <party-pubkey>' +
        '\n\t\t fwatch <filter>' +
        '\n\t\t ecwatch <filter>' +
        '\n\t\t ecdwatch <filter>'
    )
    .action((subCommand, arg, arg2) => {

        var nonEmptyObject = function (_arg) {
            return _arg ? JSON.parse(_arg) : {};
        };

        switch (subCommand) {
            case ipfs.add.name: {
                ipfs.add(arg);
                break;
            }
            case ipfs.cat.name: {
                ipfs.cat(arg);
                break;
            }
            case ipfs.ecTag.name: {
                ipfs.ecTag(arg);
                break;
            }
            case ipfs.decryptEcTag.name: {
                ipfs.decryptEcTag(arg);
                break;
            }
            case ipfs.decrypt.name: {
                ipfs.decrypt(arg);
                break;
            }
            case ipfs.delegate.name: {
                ipfs.delegate(arg, arg2);
                break;
            }
            case ipfs.signature.name: {
                ipfs.signature(arg);
                break;
            }
            case ipfs.fwatch.name: {
                ipfs.fwatch(nonEmptyObject(arg));
                break;
            }
            case ipfs.ecwatch.name: {
                ipfs.ecwatch(nonEmptyObject(arg));
                break;
            }
            case ipfs.ecdwatch.name: {
                ipfs.ecdwatch(nonEmptyObject(arg));
                break;
            }
            case ipfs.pull.name: {
                ipfs.pull(arg);
                break;
            }
            case ipfs.providers.name: {
                ipfs.providers(arg);
                break;
            }
        }

    });

program.command('config [subCmmand] [arg]')
    .description('File-force configuration.'.bold +
        '\n\t Subcommand:' +
        '\n\t\t show'
    )
    .action((command, arg, options) => {
       switch (command) {
           case config.show.name: {
               config.show(arg, options);
               break;
           }
           default: {
               config.show(arg, options);
           }
       }
    });


program.parse(process.argv); // notice that we have to parse in a new statement.


